package com.softeem.game;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.swing.JFrame;import javax.swing.JOptionPane;/** * 游戏画板 * @author Mr.Zhou * */public class GameFrame extends JFrame implements KeyListener,Runnable{private List<BackGround> allBG = new ArrayList<BackGround>();private Mario mario = null;private BackGround nowBG = null;private Thread t = null;//是否已经开始游戏private boolean isStart = false;public static void main(String[] args){GameFrame frame = new GameFrame();frame.setTitle("超级玛丽v1.0~周哥");MP3Player mp3 = new MP3Player("music/bg.mp3"); mp3.play();}//初始化public GameFrame(){//this.setTitle("玛丽奥");this.setSize(900, 600);//int width = Toolkit.getDefaultToolkit().getScreenSize().width;//int height = Toolkit.getDefaultToolkit().getScreenSize().height;//this.setLocation((width-900)/2, (height-600)/2);this.setLocationRelativeTo(null);this.setResizable(false);//初始化图片StaticValue.init();//创建全部场景for(int i=1;i<=7;i ){this.allBG.add(new BackGround(i, i==7?true:false));}//将第一个场景设置为当前场景this.nowBG = this.allBG.get(0);//初始化玛丽奥this.mario = new Mario(0);//将玛丽奥放入场景中this.mario.setBg(nowBG);this.repaint();this.addKeyListener(this);this.t = new Thread(this);t.start();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}//设置游戏标题public void setTitle(String title){super.setTitle(title);}//设置游戏窗体尺寸public void setSize(int width,int height){super.setSize(width, height);}//将玛丽奥添加到游戏中public void setRole(Mario mario){mario.setBg(nowBG);}public void paint(Graphics g) {//双缓存BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);Graphics g2 = image.getGraphics();Font font = new Font("楷体",Font.BOLD,25);if(this.isStart){//绘制背景g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);//绘制生命g2.setFont(font);g2.drawString("生命:" this.mario.getLife(), 620, 50);//绘制分数g2.drawString("分数:" this.mario.getScore(), 750, 50);//绘制怪物敌人Iterator<Enemy> iterEnemy = this.nowBG.getAllEnemy().iterator();while(iterEnemy.hasNext()){Enemy e = iterEnemy.next();g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);}//绘制障碍物Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator();while(iter.hasNext()){Obstruction ob = iter.next();g2.drawImage(ob.getShowImage(), ob.getX(), ob.getY(), this);}//绘制玛丽奥g2.drawImage(this.mario.getShowImage(), this.mario.getX(), this.mario.getY(), this);}else{g2.drawImage(StaticValue.startImage, 0, 0, this);}//把缓存图片绘制进去g.drawImage(image, 0, 0, this);}public void keyTyped(KeyEvent e) {}public void keyPressed(KeyEvent e) {if(this.isStart){//玛丽奥的移动控制if(e.getKeyCode()==39){this.mario.rightMove();}if(e.getKeyCode()==37){this.mario.leftMove();}//跳跃控制if(e.getKeyCode()==38){this.mario.jump();}}else if(e.getKeyCode()==32){this.isStart = true;this.nowBG.enemyStartMove();this.mario.setScore(0);this.mario.setLife(3);}}public void keyReleased(KeyEvent e) {if(this.isStart){//控制玛丽奥的停止if(e.getKeyCode()==39){this.mario.rightStop();;}if(e.getKeyCode()==37){this.mario.leftStop();;}}}public void run() {while(true){this.repaint();try {Thread.sleep(50);if(this.mario.getX() >= 840){//切换场景this.nowBG = this.allBG.get(this.nowBG.getSort());//将场景放入玛丽奥中this.mario.setBg(nowBG);this.nowBG.enemyStartMove();//修改马里奥坐标this.mario.setX(0);}if(this.mario.isDead()){JOptionPane.showMessageDialog(this, "游戏结束");System.exit(0);}if(this.mario.isClear()){JOptionPane.showMessageDialog(this, "恭喜游戏通关!");System.exit(0);}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
评论